package cibertec.edu.pe.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

public class LoginFilter implements Filter {
    private String loginPage = "paginas/registrarUsuario.jsf";
    private String timeoutPage = "";

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        if ((request instanceof HttpServletRequest) && (response instanceof HttpServletResponse)) {

            HttpServletRequest httpServletRequest = (HttpServletRequest) request;
            HttpServletResponse httpServletResponse = (HttpServletResponse) response;

            String requestPath = httpServletRequest.getRequestURI();
            String hola = "hola";
            boolean loginVal = requestPath.endsWith("registrarUsuario.jsf");
            if (!loginVal) {
                if (isSessionControlRequiredForThisResource(httpServletRequest)) {
                    if (isSessionInvalid(httpServletRequest)) {
                        String timeoutUrl = httpServletRequest.getContextPath() + "/" + getTimeoutPage();

                        System.out.println("Session is invalid! redirecting to timeoutpage : " + timeoutUrl);

                        httpServletResponse.sendRedirect(timeoutUrl);
                        return;
                    }
                }
            }
        }
        filterChain.doFilter(request, response);
    }

    private boolean isSessionControlRequiredForThisResource(HttpServletRequest httpServletRequest) {
        String requestPath = httpServletRequest.getRequestURI();
        boolean controlRequired = !StringUtils.contains(requestPath, getTimeoutPage());

        return controlRequired;
    }

    private boolean isSessionInvalid(HttpServletRequest httpServletRequest) {
        boolean sessionInValid = (httpServletRequest.getRequestedSessionId() != null) && !httpServletRequest.isRequestedSessionIdValid();
        return sessionInValid;
    }

    public void destroy() {

    }

    public String getTimeoutPage() {
        return timeoutPage;
    }

    public void setTimeoutPage(String timeoutPage) {
        this.timeoutPage = timeoutPage;
    }

    public String getLoginPage() {
        return loginPage;
    }

    public void setLoginPage(String loginPage) {
        this.loginPage = loginPage;
    }
}
